﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FilStartUps.FILAPI;
using System.Drawing;
using System.IO;
using System.Net;
using System.Drawing.Imaging;
using FilStartUps.DAL;

namespace FilStartUps
{
    /// <summary>
    /// Summary description for FilStartUps1
    /// </summary>
    public class FilStartUps1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            GetImageProfile(context);
        }

        private void GetImageProfile(HttpContext context)
        {
            //profileid 
            int profileid = (Convert.ToInt32(context.Request.QueryString["id"].Trim()));
            int cat = Convert.ToInt32(context.Request.QueryString["cat"].Trim());
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                if (cat == 1)
                {
                    //memberid
                    var profile = db_connect.Profiles.Where(p => p.memberidFK.Equals(profileid)).SingleOrDefault();

                    var profile_ = db_connect.Profiles.Where(p => p.profileidPK.Equals(profile.profileidPK)).SingleOrDefault();

                    byte[] imgpost;
                    context.Response.ContentType = "image/jpeg";

                    string base64string = Convert.ToBase64String(profile_.image);

                    imgpost = Convert.FromBase64String(base64string);
                    using (MemoryStream ms = new MemoryStream(imgpost, 0, imgpost.Length))
                    {
                        ms.Write(imgpost, 0, imgpost.Length);
                        context.Response.Buffer = true;
                        context.Response.BinaryWrite(imgpost);
                        ms.Dispose();
                        ms.Close();
                    }

                }
                else
                {

                    var profile = db_connect.Profiles
                                   .Where(p => p.profileidPK.Equals(profileid)).SingleOrDefault();


                    byte[] imgpost;
                    context.Response.ContentType = "image/jpeg";

                    string base64string = Convert.ToBase64String(profile.image);

                    imgpost = Convert.FromBase64String(base64string);
                    using (MemoryStream ms = new MemoryStream(imgpost, 0, imgpost.Length))
                    {
                        ms.Write(imgpost, 0, imgpost.Length);
                        context.Response.Buffer = true;
                        context.Response.BinaryWrite(imgpost);
                        ms.Dispose();
                        ms.Close();
                    }
                }
            }
        }

        //Convert To Image
        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }

        //Convert To Base64string
        public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //convert image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                //Convert byte[] to base64string
                string base64string = Convert.ToBase64String(imageBytes);
                return base64string;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}